<?php
/** 
 * Copyright (C) 2009 João Neto
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * @version router.php - 19/07/2009 - joaoneto
 * @package jloja
 */
defined('_JEXEC') or die( 'Restricted access' ); //no direct access

class JLoja_Router
{
	private $_controller = null;

	private $_task = null;

	private $_view = null;

	private $_path;

	private $_controllerDir = 'controllers';

	/**
	 * @var Doctrine_Connection
	 */
	private $_conn;

	public function __construct( )
	{
		$this->_path = dirname( __FILE__ );
		$this->_controller = $this->_getController();
	}

	public static function getModelPath()
	{
		return JPATH_COMPONENT_ADMINISTRATOR . DS . 'models';
	}

	public static function autoload( $className )
	{
		Doctrine::autoload( $className );

		JLoader::load( $className );

		$fields = str_replace( array( '_' ), array( DS ) , strtolower( $className ) );
		
		if( file_exists( JPATH_COMPONENT_ADMINISTRATOR . DS . '_lib' . DS . $fields . '.php' ) ){
			require_once JPATH_COMPONENT_ADMINISTRATOR . DS . '_lib' . DS . $fields . '.php';
		}

	}

	/**
	 * Load The Controller Classes
	 * @return JController
	 */
	public function dispatch( $loadController = true )
	{
		$this->_loadJLojaClasses();

		if( !$loadController ){
			return true;
		}
		$controllerFile = $this->_path . DS . $this->_controllerDir . DS . $this->_controller . '.php';
		if( file_exists( $controllerFile ) ){
			require_once $controllerFile;
		}
		else{
			require_once $this->_path . DS . '_lib' . DS . 'exception' . DS . 'controllernotfoundexception.php';
			throw new ControllerNotFound_Exception( sprintf( JText::_( 'CONTROLLER_NOT_FOUND' ), $this->_controller ) );
		}

		$className = $this->_getControllerClassName();
		if( !class_exists(  $className ) ){
			require_once $this->_path . DS . '_lib' . DS . 'exception' . DS . 'controllernotfoundexception.php';
			throw new ControllerNotFound_Exception( sprintf( JText::_( 'CONTROLLER_CLASS_NOT_FOUND' ), $this->_controller ) );
		}

		$class = new $className();

		return $class->execute( $this->_getOperation() );
	}

	public function dispatchAjax()
	{
		$this->_loadJLojaClasses();
	}

	private function _getController()
	{
		return strtolower( $this->_getVar( 'controller' ) );

	}

	private function _getControllerClassName()
	{
		$controllerName = $this->_getVar( 'controller', 'product' );
		$controllerClass = 'JLojaController_' . ucfirst( $controllerName );
		return $controllerClass;
	}

	private function _getOperation()
	{
		return $this->_getVar( 'task' );
	}

	/**
	 * Override the default joomla request to improve this in future
	 */
	private function _getVar( $varName, $defaultValue = null )
	{
		return JRequest::getVar( $varName, $defaultValue );
	}
	
	private function _loadJLojaClasses()
	{
		/*
		 * @TODO REMOVE THIS!
		 */
		require 'jlojacontroller.php';
		require 'jlojaview.php';
		require 'jlojamodel.php';
		require 'development.php';
		require 'jlojaloader.php';
		require 'form' . DS . 'form.php';

		$this->_connection();

	}

	private function _connection()
	{
		require_once( dirname(__FILE__) . DS . '_lib' . DS . 'doctrine' . DS . 'Doctrine.php' );
		spl_autoload_register( array( 'JLoja_Router', 'autoload' ) );
		$manager = Doctrine_Manager::getInstance();
		$dsn = 'mysql:dbname=jloja_db2;host=localhost';
		$user = 'root';
		$password = 'root';

		$dbh = new PDO($dsn, $user, $password);
		Doctrine_Manager::connection($dbh);
//		Doctrine::generateModelsFromYaml( JPATH_COMPONENT_ADMINISTRATOR . DS . 'dbAlpha.yml', dirname(__FILE__) . DS . 'models'  );
//		exit;
		Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
		Doctrine::loadModels( dirname(__FILE__) . DS . 'models' );
	}
}